﻿using Mimas.Core.Current;
using Mimas.Framework.Shared.Data.DataTransferObjects;
using Mimas.Framework.Shared.Data.EnumObjects;
using System;
using System.Linq;

namespace Mimas.Framework.Server.DomainObjects.Mapping
{
    public static class UserMapping
    {
        public static User Map(UserDTO data)
        {
            User newItem = new User();

            newItem.CellarNo = data.CellarNo;
            newItem.EMail = data.EMail;
            newItem.Fullname = data.Fullname;
            newItem.Id = data.Id;
            newItem.LastLoginDate = data.LastLoginDate;
            newItem.Password = data.Password;
            newItem.Username = data.Username;
            newItem.UserTypeId = (int)data.UserType;
            newItem.IsActive = data.IsActive;

            if (data.Id == 0)
            {
                newItem.CreatedOn = DateTime.Now;
            }

            return newItem;
        }

        public static UserDTO Map(User data)
        {
            UserDTO newItem = new UserDTO();

            newItem.CellarNo = data.CellarNo;
            newItem.EMail = data.EMail;
            newItem.Fullname = data.Fullname;
            newItem.Id = data.Id;
            newItem.LastLoginDate = data.LastLoginDate;
            newItem.Password = data.Password;
            newItem.Username = data.Username;
            newItem.UserType = (EUserType)data.UserTypeId;
            newItem.IsActive = data.IsActive;

            return newItem;
        }

        public static User[] Map(UserDTO[] data)
        {
            if (data == null || !data.Any()) return null;
            return data.Where(x => x != null).Select(x => Map(x)).ToArray();
        }

        public static UserDTO[] Map(User[] data)
        {
            if (data == null || !data.Any()) return null;
            return data.Where(x => x != null).Select(x => Map(x)).ToArray();
        }
    }
}
